#include "pagenetwork.h"
#include "ui_pagenetwork.h"
#include <QPainter>
#include "qdebug.h"

PageNetwork::PageNetwork(int type, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageNetwork)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    fculist.append(ui->fcu_1);fculist.append(ui->fcu_6);
    ecrlist.append(ui->ecr_2);ecrlist.append(ui->ecr_5);
    atclist.append(ui->atc_1);atclist.append(ui->atc_6);
    vculist.append(ui->vcu_1);vculist.append(ui->vcu_6);
    vsculist.append(ui->vscu_1);vsculist.append(ui->vscu_6);
    ddulist.append(ui->ddu_1);ddulist.append(ui->ddu_6);
    riom10list.append(ui->riom10_1);riom10list.append(ui->riom10_6);
    evrlist.append(ui->evr_4);evrlist.append(ui->evr_1);evrlist.append(ui->evr_6);
    hvaclist.append(ui->hvac_1);hvaclist.append(ui->hvac_2);hvaclist.append(ui->hvac_3);
    hvaclist.append(ui->hvac_4);hvaclist.append(ui->hvac_5);hvaclist.append(ui->hvac_6);
    bculist.append(ui->bcu_1);bculist.append(ui->bcu_3);bculist.append(ui->bcu_4);bculist.append(ui->bcu_6);
    edculist.append(ui->edcu7_1);edculist.append(ui->edcu7_2);edculist.append(ui->edcu7_3);edculist.append(ui->edcu7_4);edculist.append(ui->edcu7_5);edculist.append(ui->edcu7_6);
    edculist.append(ui->edcu8_1);edculist.append(ui->edcu8_2);edculist.append(ui->edcu8_3);edculist.append(ui->edcu8_4);edculist.append(ui->edcu8_5);edculist.append(ui->edcu8_6);
    piclist.append(ui->pic_1);piclist.append(ui->pic_6);
    bmslist.append(ui->bms_1);bmslist.append(ui->bms_6);
    acelist.append(ui->siv_1);acelist.append(ui->siv_3);acelist.append(ui->siv_4);acelist.append(ui->siv_6);
    tcdslist.append(ui->tcds_1);
    lculist.append(ui->lcu_1);lculist.append(ui->lcu_2);lculist.append(ui->lcu_3);
    lculist.append(ui->lcu_4);lculist.append(ui->lcu_5);lculist.append(ui->lcu_6);
    pcelist.append(ui->tcu_2);pcelist.append(ui->tcu_3);pcelist.append(ui->tcu_4);pcelist.append(ui->tcu_5);
    icdslist.append(ui->icds_2);icdslist.append(ui->icds_5);

    devlist.append(fculist);
    devlist.append(ecrlist);
    devlist.append(atclist);
    devlist.append(vculist);
    devlist.append(vsculist);
    devlist.append(ddulist);
    devlist.append(riom10list);
    devlist.append(evrlist);
    devlist.append(hvaclist);
    devlist.append(bculist);
    devlist.append(edculist);
    devlist.append(piclist);
    devlist.append(bmslist);
    devlist.append(acelist);
    devlist.append(tcdslist);
    devlist.append(lculist);
    devlist.append(pcelist);
    devlist.append(icdslist);
    for(int i=0; i<TOPO_COUNT; ++i)
        for(int j=0; j<devlist.at(i).size(); ++j)
            setDeviceComStatus(i, j, 0, 0);
    setTCDSVisible(false);
    net_type_ = type;
    change_to_type(net_type_);
}

PageNetwork::~PageNetwork()
{
    delete ui;
}

void PageNetwork::setDeviceComStatus(int dev, int index, bool flt, bool master)
{
    if(dev <0 || dev>=TOPO_COUNT || index<0)
        return;
    if(index < devlist.at(dev).size())
    {
        if(dev == TOPO_VCU || dev == TOPO_VSCU || dev == TOPO_PIC)
        {
            if(master)
                devlist[dev][index]->setStyleSheet(!flt ? QString("border:1px solid black; background-color:rgb(0,255,0); font-size:13pt") : QString("border:1px solid black; background-color:red; font-size:13pt"));
            else
                devlist[dev][index]->setStyleSheet(!flt ? QString("border:1px solid black; background-color:rgb(0,255,0); font-size:10pt") : QString("border:1px solid black; background-color:red; font-size:10pt"));
        }
        else
            devlist[dev][index]->setStyleSheet(!flt ? QString("border:1px solid black; background-color:rgb(0,255,0)") : QString("border:1px solid black; background-color:red"));
        if(dev == TOPO_ATC)
            xATCComFlt[index] = flt;
    }
}

bool PageNetwork::getATCComFlt(int i)
{
    Q_ASSERT(i>=0 && i<=1);
    return xATCComFlt[i];
}

void PageNetwork::setTCDSVisible(bool xVisible)
{
    ui->tcds_1->setVisible(xVisible);
    ui->line_110->setVisible(xVisible && (net_type_ == Network_BlueAndRed || net_type_ == Network_Blue));
    ui->line_115->setVisible(xVisible && (net_type_ == Network_BlueAndRed || net_type_ == Network_Red));
}

void PageNetwork::change_to_type(int type)
{
    if(type == Network_BlueAndRed)
        return;
    else if(type == Network_Red)
    {
        //Tc1
        ui->line_34->hide();ui->line_90->hide();ui->line_92->hide();ui->line_95->hide();
        ui->line_100->hide();ui->line_103->hide();ui->line_108->hide();ui->line_88->hide();
        ui->line_106->hide();ui->line_91->hide();ui->evr_1->hide();ui->label->resize(61,81);

        ui->line_87->hide();ui->line_86->hide();ui->line_93->hide();ui->line_96->hide();
        ui->line_101->hide();ui->line_22->hide();
        ui->edcu7_1->hide();
        ui->line_7->hide();ui->line_98->hide();ui->line_93->hide();ui->line_10->hide();
        //Mp1
        ui->line_41->hide();ui->line_43->hide();ui->line_45->hide();ui->line_48->hide();
        ui->line_47->hide();ui->line_50->hide();ui->line_14->hide();
        ui->edcu7_2->hide();ui->ecr_2->hide();
        //M1
        ui->line_55->hide();ui->line_53->hide();ui->line_51->hide();ui->line_62->hide();
        ui->line_58->hide();ui->line_57->hide();ui->line_16->hide();
        ui->edcu7_3->hide();
        //M2
        ui->line_18->hide();ui->line_111->hide();ui->line_71->hide();ui->line_70->hide();
        ui->line_73->hide();ui->line_69->hide();ui->line_72->hide();ui->line_74->hide();
        ui->edcu7_4->hide();
        //Mp2
        ui->line_84->hide();ui->line_82->hide();ui->line_80->hide();ui->line_81->hide();
        ui->line_85->hide();ui->line_83->hide();ui->line_17->hide();
        ui->ecr_5->hide();ui->edcu7_5->hide();
        //Tc2
        ui->line_99->hide();ui->line_9->hide();ui->line_15->hide();ui->line_23->hide();
        ui->line_114->hide();ui->line_125->hide();ui->line_113->hide();ui->line_116->hide();
        ui->line_117->hide();ui->line_38->hide();ui->line_148->hide();ui->line_121->hide();
        ui->line_123->hide();ui->line_122->hide();ui->line_112->hide();ui->line_119->hide();
        ui->line_118->hide();ui->line_120->hide();
        ui->edcu7_6->hide();
        ui->evr_6->hide();ui->label_7->resize(61,81);ui->line_126->hide();
    }
    else if(type == Network_Blue)
    {
        //Tc1
        ui->line_33->hide();ui->line_94->hide();ui->line_91->hide();ui->line_97->hide();
        ui->line_102->hide();ui->line_105->hide();ui->line_129->hide();ui->line_109->hide();
        ui->line_6->hide();ui->line_90->hide();ui->evr_1->hide();ui->label->resize(61,81);
        ui->edcu8_1->hide();
        //Mp1
        ui->line_11->hide();ui->line_40->hide();ui->line_42->hide();ui->line_44->hide();
        ui->line_46->hide();ui->line_49->hide();
        ui->edcu8_2->hide();
        //M1
        ui->line_12->hide();ui->line_52->hide();ui->line_56->hide();ui->line_61->hide();
        ui->line_60->hide();ui->line_54->hide();ui->line_59->hide();ui->line_12->hide();
        ui->edcu8_3->hide();
        //M2
        ui->line_104->hide();ui->line_64->hide();ui->line_67->hide();ui->line_63->hide();
        ui->line_65->hide();ui->line_66->hide();ui->line_13->hide();ui->line_68->hide();
        ui->edcu8_4->hide();
        //Mp2
        ui->line_76->hide();ui->line_79->hide();ui->line_77->hide();ui->line_78->hide();
        ui->line_75->hide();ui->line_19->hide();
        ui->edcu8_5->hide();
        //Tc2
        ui->line_89->hide();ui->line_37->hide();ui->line_126->hide();ui->line_130->hide();
        ui->line_127->hide();ui->line_128->hide();ui->line_8->hide();
        ui->line_144->hide();ui->line_124->hide();ui->line_145->hide();
        ui->edcu8_6->hide();
        ui->evr_6->hide();ui->label_7->resize(61,81);ui->line_121->hide();
    }
}
